/**
 * Copyright (C) 2014 xspace app Project.
 */

package cn.xspace.module.other.http.impl;

import com.xspace.android.xafcommon.network.NetConstant;
import com.xspace.android.xafcommon.network.message.AResponseMsg;
import com.xspace.android.xafcommon.network.message.SendRequestListener;

import android.content.Context;
import android.widget.Toast;
import cn.xspace.module.other.activity.RegisterAct;
import cn.xspace.module.other.http.NetControl;
import cn.xspace.module.other.http.intface.IFetchVertifyCode;
import cn.xspace.module.other.http.resp.FetchVertifyCodeResp;
import cn.xspace.module.other.util.RProxy;

/**
 * @ClassName: FetchVertifyCodeImpl
 * @author:mawen
 * @Date：2014-2-25
 * @version V1.0.0
 * @Description: TODO
 */
public class FetchVertifyCodeImpl implements SendRequestListener,
		IFetchVertifyCode {
	private NetControl netControl;
	private Context mContext;

	/**
	 * @Description: TODO
	 * @param
	 * @return
	 * @throws
	 */
	public FetchVertifyCodeImpl(Context context) {
		this.mContext = context;
		netControl = new NetControl(this);
	}

	/*
	 * <p>Title: fetchVertifyCode</p> <p>Description: </p>
	 * 
	 * @param mobile
	 * 
	 * @param isRegister
	 * 
	 * @see
	 * cn.xspace.app.other.logic.intface.IFetchVertifyCode#fetchVertifyCode(
	 * java.lang.String, boolean)
	 */
	@Override
	public void fetchVertifyCode(String mobile, boolean isRegister) {
		netControl.sendFetchVertifyCodeReq(mobile, isRegister);
	}

	/*
	 * <p>Title: requestResult</p> <p>Description: </p>
	 * 
	 * @param result
	 * 
	 * @param object
	 * 
	 * @see com.xspace.android.xafcommon.network.message.SendRequestListener#
	 * requestResult(int,
	 * com.xspace.android.xafcommon.network.message.AResponseMsg)
	 */
	@Override
	public void requestResult(int result, AResponseMsg object) {
		((RegisterAct) mContext).cancelDialog();
		FetchVertifyCodeResp resp = (FetchVertifyCodeResp) object;
		if (object.msgType.equals(NetControl.MSGTYPE_FETCH_VERTIFYCODE)) {
			if (result == NetConstant.TaskStatus.SUCCESS && resp != null
					&& resp.fetchVertifyCodeBean != null) {
				if (resp.fetchVertifyCodeBean.code
						.equals(String
								.valueOf(FetchVertifyCodeResp.FETCH_VERTIFYCODE_SUCCESS))) {// 获取成功
					((RegisterAct) mContext)
							.changeView(resp.fetchVertifyCodeBean.desc);
				} else if (resp.fetchVertifyCodeBean.code
						.equals(String
								.valueOf(FetchVertifyCodeResp.FETCH_VERTIFYCODE_REPEAT_ERROR))) {
					((RegisterAct) mContext)
							.changeViewRepeat(resp.fetchVertifyCodeBean.desc);
				} else if(resp.fetchVertifyCodeBean.code.equals("9")){
					String errorTip = resp.fetchVertifyCodeBean.desc;
					Toast.makeText(mContext, "用户已存在！（初始密码为:123456）", Toast.LENGTH_LONG)
							.show();
				}
				else{
					String errorTip = resp.fetchVertifyCodeBean.desc;
					Toast.makeText(mContext, errorTip, Toast.LENGTH_LONG)
							.show();
				}
			} else {
				Toast.makeText(mContext, RProxy.network_fail, Toast.LENGTH_LONG)
						.show();
			}
		}
	}

	/*
	 * <p>Title: offline</p> <p>Description: </p>
	 * 
	 * @param msgType
	 * 
	 * @see
	 * com.xspace.android.xafcommon.network.message.SendRequestListener#offline
	 * (java.lang.String)
	 */
	@Override
	public void offline(String msgType) {
		((RegisterAct) mContext).cancelDialog();
		Toast.makeText(mContext, RProxy.network_fail, Toast.LENGTH_SHORT)
				.show();
	}
}
